Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consistent use of convert_km and date_trunc through all dashboards, minor fixes #104

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

swiffer
Copy link
Contributor

@swiffer swiffer commented Nov 2, 2024

this:

  • replaces case statements with convert_km through all dashboards
  • ensures date_trunc is working correctly according to timezone in grafana and stop truncing where not neccessary
  • adresses some minor issues / improvements
    • Amortization Tracker was using hard coded Loss instead of Dashboard variable
    • also in Amortization Tracker: removes unneeded conversion (km/mi) for Loss
    • Charging Cost Stats: charge_energy_added instead of 2x charge_energy_used

added comments within PR to highlight the minor issues / imrpvoements

Dashboards have been exported using Grafana 11.2.3 (will be available shortly via teslamate-org/teslamate#4338)

Once all PR have been merged I'll export all Dashboards again with Grafana 11.2.3.

@@ -342,7 +367,7 @@
"editorMode": "code",
"format": "table",
"rawQuery": true,
"rawSql": "WITH periodic_mileage AS (\r\n SELECT\r\n DATE_TRUNC('$period', start_date) AS period_start,\r\n convert_km(SUM(end_km - start_km)::numeric, '$length_unit') AS total_mileage\r\n FROM\r\n drives\r\n WHERE\r\n car_id = $car_id \r\n GROUP BY\r\n period_start\r\n),\r\ncum_mileage AS (\r\n SELECT\r\n ROW_NUMBER() OVER () AS period_number,\r\n period_start,\r\n total_mileage,\r\n SUM(total_mileage) OVER (ORDER BY period_start) AS cum_mileage\r\n FROM\r\n periodic_mileage\r\n),\r\nBreakEven AS (\r\nSELECT\r\n cm.period_number,\r\n cm.cum_mileage * (0.20 / CASE WHEN '$length_unit' = 'mi' THEN 1 ELSE 1.60934 END) AS depreciation_mileage,\r\n ($depreciated_value - (cm.period_number - 1) * 5.0 / 12) AS depreciation_time\r\nFROM\r\n cum_mileage cm\r\nLEFT JOIN\r\n periodic_mileage mm ON cm.period_start = mm.period_start\r\nORDER BY\r\n cm.period_number\r\n)\r\nSELECT \r\n period_number,\r\n ($car_cost - depreciation_mileage - (($car_cost - depreciation_mileage) * depreciation_time / 100)) * 100 / $car_cost AS percent_depreciated\r\nFROM BreakEven",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loss_rate was hard coded to 0.20 here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the length_unit correction for miles shouldnt be neccessary here.

@@ -640,7 +665,7 @@
"editorMode": "code",
"format": "table",
"rawQuery": true,
"rawSql": "WITH periodic_mileage AS (\r\n SELECT\r\n DATE_TRUNC('$period', start_date) AS period_start,\r\n convert_km(SUM(end_km - start_km)::numeric, '$length_unit') AS total_mileage\r\n FROM\r\n drives\r\n WHERE\r\n car_id = $car_id \r\n GROUP BY\r\n period_start\r\n),\r\nperiod_cost AS (\r\n SELECT\r\n DATE_TRUNC('$period', start_date) AS period_start,\r\n SUM(cost) AS total_cost\r\n FROM\r\n charging_processes \r\n WHERE\r\n car_id = $car_id \r\n GROUP BY\r\n DATE_TRUNC('$period', start_date)\r\n),\r\ncum_mileage AS (\r\n SELECT\r\n ROW_NUMBER() OVER () AS period_number,\r\n period_start,\r\n total_mileage,\r\n SUM(total_mileage) OVER (ORDER BY period_start) AS cum_mileage\r\n FROM\r\n periodic_mileage\r\n),\r\ncum_cost AS (\r\n SELECT\r\n ROW_NUMBER() OVER () AS period_number,\r\n period_start,\r\n total_cost,\r\n SUM(total_cost) OVER (ORDER BY period_start) AS cum_el_cost\r\n FROM\r\n period_cost\r\n),\r\ncar_current_value AS (\r\n SELECT\r\n $car_cost AS car_current_value\r\n),\r\nBreakEven AS (\r\nSELECT\r\n cm.period_number,\r\n mm.total_mileage AS periodic_mileage,\r\n mc.total_cost AS period_el_cost,\r\n mc.total_cost/mm.total_mileage AS period_cost_per_mileage,\r\n cm.cum_mileage,\r\n cc.cum_el_cost,\r\n cm.cum_mileage * ($loss_rate / CASE WHEN '$length_unit' = 'mi' THEN 1 ELSE 1.60934 END) AS depreciation_mileage,\r\n ($depreciated_value - (cm.period_number - 1) * 5.0 / CASE WHEN '$period' = 'year' THEN 1 ELSE 12 END) AS depreciation_time,\r\n cm.cum_mileage * $fuel_price - cc.cum_el_cost AS cum_el_savings, car_current_value\r\nFROM\r\n cum_mileage cm\r\nLEFT JOIN\r\n cum_cost cc ON cm.period_number = cc.period_number\r\nLEFT JOIN\r\n periodic_mileage mm ON cm.period_start = mm.period_start\r\nLEFT JOIN\r\n period_cost mc ON cm.period_start = mc.period_start\r\nCROSS JOIN\r\n car_current_value\r\nORDER BY\r\n cm.period_number\r\n)\r\nSELECT \r\n period_number, periodic_mileage, period_el_cost, period_cost_per_mileage, cum_mileage, cum_el_cost, depreciation_mileage, depreciation_time, cum_el_savings,\r\n car_current_value - depreciation_mileage - ((car_current_value - depreciation_mileage) * depreciation_time / 100) AS depreciated_car_value,\r\n (car_current_value - depreciation_mileage - ((car_current_value - depreciation_mileage) * depreciation_time / 100)) + (cum_mileage * $fuel_price - cum_el_cost) AS car_value_pls_el_savings\r\nFROM BreakEven",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the length_unit correction for miles shouldnt be neccessary here.

@@ -3229,7 +3265,7 @@
"hide": false,
"metricColumn": "none",
"rawQuery": true,
"rawSql": "WITH data AS (\n SELECT\n drives.*,\n date_trunc('month', TIMEZONE('UTC', start_date)) as local_period\n FROM drives)\nSELECT\n EXTRACT(EPOCH FROM date_trunc('month', local_period))*1000 AS date_from,\n EXTRACT(EPOCH FROM date_trunc('month', local_period + ('1 ' || 'month')::INTERVAL))*1000 AS date_to,\n CASE 'month'\n WHEN 'month' THEN to_char(local_period, 'YYYY Month')\n WHEN 'year' THEN to_char(local_period, 'YYYY')\n WHEN 'week' THEN 'week ' || to_char(local_period, 'WW') || ' starting ' || to_char(local_period, 'YYYY-MM-DD')\n ELSE to_char(local_period, 'YYYY-MM-DD')\n END AS display,\n local_period AS date,\n sum(GREATEST(start_${preferred_range}_range_km - end_${preferred_range}_range_km, 0) * car.efficiency * 1000) / \n convert_km(sum(distance)::numeric, '$length_unit') as efficiency_net_$length_unit\nFROM data\nJOIN cars car ON car.id = car_id\nWHERE\n car_id = $car_id\nGROUP BY date\nORDER BY date",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case statement not needed, only month available.

@@ -3180,7 +3216,7 @@
"hide": false,
"metricColumn": "none",
"rawQuery": true,
"rawSql": "WITH data AS (\n SELECT\n charging_processes.*,\n date_trunc('month', TIMEZONE('UTC', start_date)) as local_period\n FROM charging_processes)\nSELECT\n EXTRACT(EPOCH FROM date_trunc('month', local_period))*1000 AS date_from,\n EXTRACT(EPOCH FROM date_trunc('month', local_period + ('1 ' || 'month')::INTERVAL))*1000 AS date_to,\n CASE 'month'\n WHEN 'month' THEN to_char(local_period, 'YYYY Month')\n WHEN 'year' THEN to_char(local_period, 'YYYY')\n WHEN 'week' THEN 'week ' || to_char(local_period, 'WW') || ' starting ' || to_char(local_period, 'YYYY-MM-DD')\n ELSE to_char(local_period, 'YYYY-MM-DD')\n END AS display,\n local_period AS date,\n sum(greatest(charge_energy_used,charge_energy_used)) AS sum_consumption_kwh,\n sum(greatest(charge_energy_used,charge_energy_used)) / count(*) AS avg_consumption_kwh,\n sum(cost) AS cost_charges,\n count(*) AS cnt_charges\nFROM data WHERE\n car_id = $car_id AND\n (charge_energy_used IS NULL OR charge_energy_used > 0.1)\nGROUP BY date\nORDER BY date",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

charge_energy_used,charge_energy_used instead of charge_energy_added,charge_energy_used

@swiffer swiffer force-pushed the convert_km_date_trunc_minor_fixes branch from c6f9c89 to e6be94f Compare November 2, 2024 16:03
@swiffer swiffer marked this pull request as draft November 16, 2024 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant